package callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @program: javase
 * @description:
 * @author: lld
 * @create: 2021-04-18 19:18
 **/
public class CreateThread {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread myThread = new MyThread();
//        FutureTask 是Runnable 实现类，MyThread是Callable实现类
//        MyThread 作为 参数 传给FutureTask
        FutureTask<String> futureTask = new FutureTask<>(myThread);
//        结果会缓存
        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start();
        new Thread(futureTask,"C").start();

        String s = futureTask.get();//获取返回值，可能会阻塞
        System.out.println(s);
    }
}
class MyThread implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println("call");
//        Thread.yield();//让出线程，让同优先级线程可以有机会，不阻塞，放入就绪队列可能再次调度
        return "1024";
    }

}
